package u1;

import static java.lang.System.*;
import static u1.Util.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Like TitleNumbers but produces a map between Netflix movie IDs and title
 * ordinals used in the adjacency list. Takes a titles.with_ids file (one
 * (numeric id, title pair) per line, with a space between the id and the title)
 * as its first argument and a titles list (eg. from Titles.java) as its second
 * argument. Outputs one (Netflix movie ID, movie ordinal) pair per line, where
 * the two numbers are separated by a space, on standard out. Both files are
 * assumed to be UTF-8-encoded. The ordinal of the first title is zero (not
 * one).
 */
public class MovieIdNumbers {
  public static void main(String[] args) throws IOException {
    if (args.length != 2) {
      err.println("See comments for usage.");
      exit(-1);
    }
    File probeFile = new File(args[0]);
    File refFile = new File(args[1]);

    /*
     * Read in the reference file and index it.
     */
    trace("Reading reference titles from %s...", refFile);
    BufferedReader titles = new BufferedReader(new InputStreamReader(
        new FileInputStream(refFile), "UTF-8"));
    Map<String, Integer> ordinals = readStringIndex(titles);
    titles.close();
    trace("Read %d titles.", ordinals.size());

    /*
     * Read in the probe file and look up each ordinal.
     */
    trace("Looking up probe movie IDs and titles from %s...", probeFile);
    BufferedReader br = new BufferedReader(new InputStreamReader(
        new FileInputStream(probeFile), "UTF-8"));
    String line = br.readLine();
    // Specifying DOTALL handles nasty unicode titles.
    Pattern p = Pattern.compile("(\\d+)(.*)$", Pattern.DOTALL);
    while (line != null) {
      Matcher m = p.matcher(line);
      if (m.matches()) {
        Integer id = Integer.valueOf(m.group(1));
        String title = m.group(2).trim();

        Integer ord = ordinals.get(canonicalizeLink(title));
        if (ord == null) {
          // There are some problems with the titles file; try a
          // case-insensitive lookup.
          String titleLower = title.toLowerCase();
          for (String key : ordinals.keySet()) {
            if (titleLower.equals(key.toLowerCase())) {
              ord = ordinals.get(key);
              trace("Found title '%s' as '%s'.", title, key);
            }
          }
        } 
        
        if (ord == null) {
          trace("Could not find title '%s' even without case sensitivity.", title);
        } else {
          out.print(id);
          out.print(" ");
          out.println(ord);
        }
      } else {
        trace("Unexpected format in line '%s'.", line);
      }
      line = br.readLine();
    }
    br.close();
    trace("Done.", refFile);
  }
}
/*
* Copyright (c) 2009 John Lees-Miller
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

